package TecMaster.Ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import TecMaster.Cliente;

/**
 * Classe responsavel pela tela de cadastrar cliente.
 * 
 * @author Flavio Ramalho
 * 
 */

public class CadastrarCliente extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JLabel labelNome, labelEndereco, labelCpf, label;
	private JPanel panel;
	private JTextField tfNome, tfCpf, tfEndereco;;
	private AbstractButton botaoAvancar, botaoCancelar;
	private Opcoes opcoes;
	private Cliente cliente;

	/**
	 * Construtor de cadastrarCliente
	 * 
	 * @param telaDeOpcoes
	 *            a tela de opcoes
	 */
	public CadastrarCliente(Opcoes telaDeOpcoes) {
		super("TecMaster - Cadastrar Cliente");
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("/imagens/TM.png")));
		this.opcoes = telaDeOpcoes;
		panel = new JPanel(null);
		panel.setBackground(Color.LIGHT_GRAY);
		setBounds(new java.awt.Rectangle(0, 0, 400, 500));
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(panel, BorderLayout.CENTER);

		Font fonte = new Font("corbel", Font.PLAIN, 15);

		label = new JLabel("Cadastro de Cliente");
		panel.add(label);
		label.setFont(new Font("corbel", Font.BOLD, 18));
		label.setBounds(120, 35, 190, 30);

		labelNome = new JLabel("Nome: ");
		panel.add(labelNome);
		labelNome.setFont(fonte);
		labelNome.setBounds(35, 90, 70, 15);

		tfNome = new JTextField();
		panel.add(tfNome);
		tfNome.setBounds(40, 110, 310, 23);

		labelEndereco = new JLabel("Endereco: ");
		panel.add(labelEndereco);
		labelEndereco.setFont(fonte);
		labelEndereco.setBounds(35, 170, 75, 15);

		tfEndereco = new JTextField();
		panel.add(tfEndereco);
		tfEndereco.setBounds(40, 190, 310, 23);

		labelCpf = new JLabel("CPF - Apenas numeros (XXXXXXXXXX): ");
		panel.add(labelCpf);
		labelCpf.setFont(fonte);
		labelCpf.setBounds(35, 250, 280, 17);

		tfCpf = new JTextField();
		panel.add(tfCpf);
		tfCpf.setBounds(40, 270, 85, 23);

		botaoAvancar = new JButton("Avancar");
		botaoAvancar.setIcon(new ImageIcon(getClass().getResource(
				"/imagens/arrow_right.png")));
		panel.add(botaoAvancar);
		botaoAvancar.setBounds(230, 350, 100, 40);
		botaoAvancar.addActionListener(this);

		botaoCancelar = new JButton("Cancelar");
		botaoCancelar.setIcon(new ImageIcon(getClass().getResource(
				"/imagens/cancel.png")));
		panel.add(botaoCancelar);
		botaoCancelar.setBounds(60, 350, 100, 40);
		botaoCancelar.addActionListener(this);

		pack();
		setSize(400, 500);
		setResizable(false);
		setVisible(true);
		setLocationRelativeTo(null);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				getOpcoes().setVisible(true);
				dispose();
			}
		});
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == botaoCancelar) {
			getOpcoes().setVisible(true);
			dispose();
		}
		if (e.getSource() == botaoAvancar) {
			try {
				cliente = new Cliente(tfNome.getText(), tfEndereco.getText(),
						tfCpf.getText());
				if (getOpcoes().checaClienteJaCadastrado(cliente))
					throw new Exception("Cliente ja cadastrado!");
				new CadastrarEquipamento(getOpcoes(), getCliente(), null);
				dispose();
			} catch (Exception eCliente) {
				JOptionPane.showMessageDialog(null, eCliente.getMessage(),
						"ERRO!", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	/**
	 * Retorna o cliente
	 * 
	 * @return o cliente
	 */
	public Cliente getCliente() {
		return cliente;
	}

	/**
	 * Retorna a tela de op�oes
	 * 
	 * @return a tela de opcoes
	 */
	public Opcoes getOpcoes() {
		return opcoes;
	}

}
